using MicroMsg.Common.Event;
using MicroMsg.Manager;
using MicroMsg.Plugin.FaceBook;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class ListSearchItem : UserControl
	{
		private const string TAG = "ListSearchItem";
		private OpenReadCompletedEventHandler imgHandler;
		public static DependencyProperty ItemObjectProperty = DependencyProperty.Register("ItemObject", typeof(SearchRetItem), typeof(ListSearchItem), new PropertyMetadata(null, new PropertyChangedCallback(ListSearchItem.OnItemObjectPropertyChanged)));
		private Run txt1;
		private Run txt2;
		private Run txt3;
		private bool mBIsFirstLoaded = true;
		private EventWatcher mWatchxxx;
		private object mObjectWatcher;
		private bool mBHasAddTag = true;
		private bool mBIsAddTagShow;
		private static List<ListSearchItem> sListSearchItem = new List<ListSearchItem>();
		internal UserControl thisCtrl;
		internal Button btnLayoutRoot;
		internal Image imgHead;
		internal Grid panelText;
		internal TextBlock textBlock_0;
		internal StackPanel panelAddArea;
		internal Image imgHasWeixin;
		internal TextBlock txtNotAdded;
		private bool _contentLoaded;
		public SearchRetItem ItemObject
		{
			get
			{
				return (SearchRetItem)base.GetValue(ListSearchItem.ItemObjectProperty);
			}
			set
			{
				base.SetValue(ListSearchItem.ItemObjectProperty, value);
			}
		}
		public ListSearchItem()
		{
			this.InitializeComponent();
			ListSearchPage.AllShakePeopleItems.Add(this);
		}
		private static void OnItemObjectPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == ListSearchItem.ItemObjectProperty)
			{
				(dependencyObject_0 as ListSearchItem).ItemObjectPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void initRuns()
		{
			new List<Run>();
			int num = 0;
			using (IEnumerator<Inline> enumerator = this.textBlock_0.get_Inlines().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					Inline current = enumerator.get_Current();
					Run run = current as Run;
					if (run != null && run.get_Foreground() == MMTheme.GetInstance().LISTB_NAME_WORD_BRUSH)
					{
						break;
					}
					num++;
				}
			}
			if (num + 2 < this.textBlock_0.get_Inlines().get_Count())
			{
				this.txt1 = (this.textBlock_0.get_Inlines().get_Item(num) as Run);
				this.txt2 = (this.textBlock_0.get_Inlines().get_Item(num + 1) as Run);
				this.txt3 = (this.textBlock_0.get_Inlines().get_Item(num + 2) as Run);
			}
			this.txt1.set_Text("");
			this.txt2.set_Text("");
			this.txt3.set_Text("");
		}
		private void ItemObjectPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue() && this.ItemObject != null)
			{
				if (this.ItemObject.mStrKey == null)
				{
					return;
				}
				this.initRuns();
				this.setItemStyle();
				string arg_43_0 = this.textBlock_0.get_Text();
				if (this.ItemObject.mMatchRange.mNEndIndex < this.ItemObject.mStrKey.get_Length() && this.ItemObject.mMatchRange != null && this.ItemObject.mMatchRange.mNStartIndex <= this.ItemObject.mMatchRange.mNEndIndex && this.ItemObject.mMatchRange.mNStartIndex >= 0)
				{
					this.txt1.set_Text("");
					if (this.ItemObject.mMatchRange.mNStartIndex != 0)
					{
						this.txt1.set_Text(this.ItemObject.mStrKey.Substring(0, this.ItemObject.mMatchRange.mNStartIndex));
					}
					this.txt2.set_Text(this.ItemObject.mStrKey.Substring(this.ItemObject.mMatchRange.mNStartIndex, this.ItemObject.mMatchRange.mNEndIndex - this.ItemObject.mMatchRange.mNStartIndex + 1));
					if (this.ItemObject.mMatchRange.mNEndIndex + 1 < this.ItemObject.mStrKey.get_Length())
					{
						this.txt3.set_Text(this.ItemObject.mStrKey.Substring(this.ItemObject.mMatchRange.mNEndIndex + 1));
					}
				}
				else
				{
					if (this.ItemObject.mMatchRange != null && this.ItemObject.mMatchRange.mNStartIndex == -1)
					{
						this.txt1.set_Text(this.ItemObject.mStrKey);
					}
				}
				if (this.ItemObject.mObject == null)
				{
					return;
				}
				if (this.ItemObject.mObject is Contact)
				{
					Contact contact = this.ItemObject.mObject as Contact;
					BitmapImage cacheHeadImage = HeadImgMgr.getCacheHeadImage(contact);
					if (cacheHeadImage != HeadImgMgr.defaultHeadImage)
					{
						this.imgHead.set_Source(cacheHeadImage);
					}
					else
					{
						this.imgHead.set_Source(HeadImgMgr.getHeadImage(contact));
					}
					this.hideAddTag();
					return;
				}
				if (this.ItemObject.mObject is QFriend)
				{
					QFriend qFriend = this.ItemObject.mObject as QFriend;
					BitmapImage cacheHeadImage2 = HeadImgMgr.getCacheHeadImage(qFriend);
					if (cacheHeadImage2 != HeadImgMgr.defaultQQHeadImage)
					{
						this.imgHead.set_Source(cacheHeadImage2);
					}
					else
					{
						this.imgHead.set_Source(HeadImgMgr.getHeadImage(qFriend));
					}
					if (this.isQItemFromOffMsg(qFriend))
					{
						this.hideAddTag();
						return;
					}
					if (qFriend.nWeixinStatus == 0u)
					{
						this.hideAddTag();
						return;
					}
					if (qFriend.nWeixinStatus == 2u)
					{
						this.showAddTag(true);
						return;
					}
					this.showAddTag(false);
					return;
				}
				else
				{
					if (this.ItemObject.mObject is MobileContact)
					{
						MobileContact mobileContact = this.ItemObject.mObject as MobileContact;
						if (mobileContact.nStatus == 2)
						{
							this.showAddTag(true);
							return;
						}
						if (mobileContact.nStatus == 1)
						{
							this.showAddTag(false);
							return;
						}
						this.hideAddTag();
						return;
					}
					else
					{
						if (this.ItemObject.mObject is FBContact)
						{
							FBContact fBContact = this.ItemObject.mObject as FBContact;
							BitmapImage cache = FaceBookMgr.getCache(fBContact.nID);
							if (cache != null)
							{
								this.imgHead.set_Source(cache);
							}
							else
							{
								this.imgHead.set_Source(HeadImgMgr.defaultHeadImage);
								string text = string.Format("http://graph.facebook.com/{0}/picture", fBContact.nID);
								WebClient webClient = new WebClient();
								webClient.add_OpenReadCompleted(new OpenReadCompletedEventHandler(this.w_OpenReadCompleted));
								webClient.OpenReadAsync(new Uri(text), fBContact.nID);
							}
							if (fBContact.nStatus == 2)
							{
								this.showAddTag(true);
								return;
							}
							if (fBContact.nStatus == 1)
							{
								this.showAddTag(false);
								return;
							}
							this.hideAddTag();
							return;
						}
						else
						{
							this.imgHead.set_Source(HeadImgMgr.defaultHeadImage);
						}
					}
				}
			}
		}
		private void w_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
		{
			FBContact fBContact = this.ItemObject.mObject as FBContact;
			if (fBContact == null)
			{
				return;
			}
			ulong num = (ulong)e.get_UserState();
			BitmapImage bitmapImage = null;
			try
			{
				bitmapImage = FaceBookMgr.addCache(num, e.get_Result());
			}
			catch (Exception)
			{
			}
			if (num != fBContact.nID)
			{
				return;
			}
			if (bitmapImage != null)
			{
				this.imgHead.set_Source(bitmapImage);
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void registerEvent()
		{
			if (this.mWatchxxx == null)
			{
				this.mWatchxxx = new EventWatcher(this, null, new EventHandlerDelegate(this.Onxxx));
				EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
			}
		}
		private void unRegisterEvent()
		{
			if (this.mWatchxxx != null)
			{
				EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CONTACT_ADD, this.mWatchxxx);
				this.mWatchxxx = null;
			}
		}
		private void Onxxx(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (evtArgs == null)
			{
				return;
			}
		}
		private bool isQItemFromOffMsg(QFriend qqfriend)
		{
			if (qqfriend != null && this.ItemObject != null)
			{
				bool result = false;
				if (this.ItemObject.mObjTag != null)
				{
					result = (bool)this.ItemObject.mObjTag;
				}
				return result;
			}
			return false;
		}
		private void btnLayoutRoot_Click(object sender, RoutedEventArgs e)
		{
			SearchRetItem itemObject = this.ItemObject;
			if (this.ItemObject == null)
			{
				return;
			}
			if (itemObject.mObject is Contact)
			{
				ContactsView.GoToContactDetailPage(itemObject.mObject as Contact);
				return;
			}
			if (itemObject.mObject is QFriend)
			{
				QFriend qFriend = itemObject.mObject as QFriend;
				if (qFriend == null)
				{
					return;
				}
				bool flag = this.isQItemFromOffMsg(qFriend);
				object[] object_ = new object[]
				{
					qFriend
				};
				if (flag)
				{
					ChatContainerPage.GoToChatPage(ToolFunction.GetRootPage(), qFriend.nQQ.ToString() + "@qqim", null, null, null);
					return;
				}
				if (qFriend.nWeixinStatus == 0u)
				{
					ToolFunction.GotoPageForUserControl("/Source/Plugin/ViewQQInfo/UI/QQFriendInfoWeixinOff.xaml", object_, false);
					return;
				}
				ToolFunction.GotoPageForUserControl("/Source/Plugin/ViewQQInfo/UI/QQFriendInfo.xaml", object_, false);
				return;
			}
			else
			{
				if (itemObject.mObject is MobileContact)
				{
					MobileContact mobileCon = itemObject.mObject as MobileContact;
					ContactDetailViewForAll.GotoContactDitailView(mobileCon, false);
					return;
				}
				if (itemObject.mObject is FBContact)
				{
					FBContact fBContact = itemObject.mObject as FBContact;
					PhoneApplicationFrame phoneApplicationFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
					BasePage basePage = phoneApplicationFrame.get_Content() as BasePage;
					if (basePage == null)
					{
						return;
					}
					if (fBContact.nStatus == 2)
					{
						ContactDetailViewForAll.GotoContactDitailView(fBContact, false);
						return;
					}
					if (fBContact.nStatus == 1)
					{
						ContactDetailViewForAll.GotoContactDitailView(fBContact, false);
						return;
					}
					InviteFriends.StartInvite(basePage, fBContact);
				}
				return;
			}
		}
		private void showAddTag(bool bIsAdded)
		{
			if (!this.mBHasAddTag)
			{
				this.getAddTag();
			}
			if (bIsAdded)
			{
				this.txtNotAdded.set_Visibility(1);
			}
			else
			{
				this.txtNotAdded.set_Visibility(0);
			}
			if (this.mBIsAddTagShow)
			{
				return;
			}
			this.mBIsAddTagShow = true;
			this.panelAddArea.set_Visibility(0);
		}
		private void hideAddTag()
		{
			if (!this.mBIsAddTagShow)
			{
				return;
			}
			this.mBIsAddTagShow = false;
			this.panelAddArea.set_Visibility(1);
		}
		private bool isAddTagShow()
		{
			return this.mBIsAddTagShow;
		}
		private bool hasAddTag()
		{
			return true;
		}
		private void getAddTag()
		{
			if (this.mBHasAddTag)
			{
				return;
			}
			this.mBHasAddTag = true;
		}
		private void setItemStyle()
		{
			if (this.ItemObject.mObject is QFriend)
			{
				this.setStyleQCon();
				return;
			}
			if (this.ItemObject.mObject is MobileContact)
			{
				this.setStyleMCon();
				return;
			}
			if (this.ItemObject.mObject is FBContact)
			{
				this.setStyleFbCon();
				return;
			}
			this.setStyleDefault();
		}
		private void setTextFontSize(int nFontSize)
		{
			this.btnLayoutRoot.set_FontSize((double)nFontSize);
		}
		private void setStyleDefault()
		{
			this.imgHead.set_Visibility(0);
			this.panelText.set_VerticalAlignment(1);
			this.panelText.set_MaxWidth(330.0);
			this.panelAddArea.set_VerticalAlignment(0);
			this.imgHasWeixin.set_Height(18.0);
			this.imgHasWeixin.set_Width(18.0);
			this.txtNotAdded.set_FontSize(20.0);
			this.setTextFontSize(35);
			this.panelText.set_Height(72.0);
		}
		private void setStyleCon()
		{
			this.setStyleDefault();
		}
		private void setStyleQCon()
		{
			this.panelText.set_Height(84.0);
			this.imgHead.set_Visibility(0);
			this.panelText.set_VerticalAlignment(1);
			this.panelText.set_MaxWidth(270.0);
			this.panelAddArea.set_VerticalAlignment(0);
			this.imgHasWeixin.set_Height(18.0);
			this.imgHasWeixin.set_Width(18.0);
			this.txtNotAdded.set_FontSize(20.0);
			this.setTextFontSize(30);
		}
		private void setStyleMCon()
		{
			this.panelText.set_Height(72.0);
			this.imgHead.set_Visibility(1);
			this.panelText.set_VerticalAlignment(1);
			this.panelText.set_MaxWidth(200.0);
			this.panelAddArea.set_VerticalAlignment(1);
			this.imgHasWeixin.set_Height(27.0);
			this.imgHasWeixin.set_Width(29.0);
			this.txtNotAdded.set_FontSize(20.0);
			this.setTextFontSize(35);
		}
		private void setStyleFbCon()
		{
			this.panelText.set_Height(72.0);
			this.imgHead.set_Visibility(0);
			this.panelText.set_VerticalAlignment(0);
			this.panelText.set_MaxWidth(200.0);
			this.panelAddArea.set_VerticalAlignment(1);
			this.imgHasWeixin.set_Height(33.0);
			this.imgHasWeixin.set_Width(33.0);
			this.txtNotAdded.set_FontSize(20.0);
			this.setTextFontSize(30);
		}
		public static ListSearchItem GetOneItem()
		{
			if (ListSearchItem.sListSearchItem != null && ListSearchItem.sListSearchItem.get_Count() != 0)
			{
				ListSearchItem result = Enumerable.First<ListSearchItem>(ListSearchItem.sListSearchItem);
				ListSearchItem.sListSearchItem.RemoveAt(0);
				return result;
			}
			return new ListSearchItem();
		}
		public static void PushOneItem(ListSearchItem item)
		{
			if (ListSearchItem.sListSearchItem == null)
			{
				ListSearchItem.sListSearchItem = new List<ListSearchItem>();
			}
			ListSearchItem.sListSearchItem.Add(item);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/ListSearchHelper/ListSearchItem.xaml", 2));
			this.thisCtrl = (UserControl)base.FindName("thisCtrl");
			this.btnLayoutRoot = (Button)base.FindName("btnLayoutRoot");
			this.imgHead = (Image)base.FindName("imgHead");
			this.panelText = (Grid)base.FindName("panelText");
			this.textBlock_0 = (TextBlock)base.FindName("txt");
			this.panelAddArea = (StackPanel)base.FindName("panelAddArea");
			this.imgHasWeixin = (Image)base.FindName("imgHasWeixin");
			this.txtNotAdded = (TextBlock)base.FindName("txtNotAdded");
		}
	}
}
